package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.JDBCUtils;
import com.arong.swing.db.entity.EquiptMonitor;
import com.arong.swing.db.entity.PagingInfo;
import com.arong.swing.db.entity.PagingResult;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
import com.arong.swing.util.StringUtil;
public class EquiptMonitorSDAO  extends BaseDAO {
	public EquiptMonitorSDAO(){
		this(DB_DEVRECORDUPLOAD);
	}
	public EquiptMonitorSDAO(String dbFile){
		super(dbFile);
	}
	public EquiptMonitorSDAO(JDBCUtils jdbcUtils){
		super(jdbcUtils);
	}
	public String getCreateTableSql(){
		return "CREATE TABLE IF NOT EXISTS "+
				"EQUIPT_MONITOR("+
						 " ID  VARCHAR2(32) PRIMARY KEY,"+
						 " EQT_ID VARCHAR2(32) NOT NULL,"+
						 " ACTIVE_TYPE  VARCHAR2(32) NOT NULL,"+
						 " ACTIVE_TIME DATE NOT NULL,"+
						 " ACTIVE_RESULT CHAR(1) NOT NULL,"+
						 " ACTIVE_DETAIL  VARCHAR2(512)"+
						 ")";
		
	}
	
	
	/**
	 * 插入
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int insert(EquiptMonitor entity) throws SysException,AppException {
		int i=0;
		try {
		 		    if (StringUtil.isBlank(entity.getId())) {
				entity.setId(getSeqValue());
			}
		 			i= jdbcUtils.insert(entity);
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}
	public  long count() throws SysException,AppException {
		long i=0;
		try {
		 	  i= jdbcUtils.queryNumValue("select count(*) from EQUIPT_MONITOR");
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}
	/**
	 * 修改
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int update(EquiptMonitor entity) throws SysException,AppException {
		int i=0;
		try {
			i=jdbcUtils.update(entity, "id");
		} catch (SQLException e) {
			throw new SysException("update error",e);
		}
		return i;
	}

	/**
	 * 删除
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int delete(EquiptMonitor entity) throws SysException,AppException {
		int i = 0;
		try {
			i=jdbcUtils.execute("DELETE FROM EQUIPT_MONITOR WHERE ID='"
					    +entity.getId()
		    		    +"'"
			);
		} catch (SQLException e) {
			throw new SysException("delete error", e);
		}
		return i;
	}

	/**
	 * 分页查询
	 * @param sql
	 * @param pagingInfo
	 * @param entity
	 * @return 
	 * @throws SysException, AppException
	 */
	public  PagingResult<EquiptMonitor> queryList(PagingInfo pagingInfo, EquiptMonitor entity)
			throws SysException,AppException {
		PagingResult<EquiptMonitor> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), EquiptMonitor.class,
				pagingInfo, getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList分页查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询 集合
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public   List<EquiptMonitor> queryList(EquiptMonitor entity) throws SysException,AppException {
	    List<EquiptMonitor> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), EquiptMonitor.class,
			getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList集合查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询对象
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  EquiptMonitor queryBean(EquiptMonitor entity) throws SysException,AppException {
		EquiptMonitor res=null;
	    try {
			res=  jdbcUtils.queryBean(getSql(entity), EquiptMonitor.class,
				getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}

	/**
	 * 获取查询条件
	 * @return
	 */
	private Object[] getParams(EquiptMonitor equiptMonitor) {
		List<Object> qryList = new ArrayList<Object>();
		if (equiptMonitor.getId() != null) {
			qryList.add(equiptMonitor.getId());
		}
		if (equiptMonitor.getEqtId() != null) {
			qryList.add(equiptMonitor.getEqtId());
		}
		if (equiptMonitor.getActiveType() != null) {
			qryList.add(equiptMonitor.getActiveType());
		}
		if (equiptMonitor.getActiveTime() != null) {
			qryList.add(equiptMonitor.getActiveTime());
		}
		if (equiptMonitor.getActiveResult() != null) {
			qryList.add(equiptMonitor.getActiveResult());
		}
		if (equiptMonitor.getActiveDetail() != null) {
			qryList.add(equiptMonitor.getActiveDetail());
		}
		return jdbcUtils.setQryList(qryList);
	}

	/**
	 * 获取sql
	 * @return
	 */
	private String getSql(EquiptMonitor equiptMonitor) {
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" a.ID,a.EQT_ID,a.ACTIVE_TYPE,a.ACTIVE_TIME,a.ACTIVE_RESULT,a.ACTIVE_DETAIL");
		sql.append(" from EQUIPT_MONITOR a where 1=1");
		if (equiptMonitor.getId() != null) {
			sql.append(" and ID=?");
		}
		if (equiptMonitor.getEqtId() != null) {
			sql.append(" and EQT_ID=?");
		}
		if (equiptMonitor.getActiveType() != null) {
			sql.append(" and ACTIVE_TYPE=?");
		}
		if (equiptMonitor.getActiveTime() != null) {
			sql.append(" and ACTIVE_TIME=?");
		}
		if (equiptMonitor.getActiveResult() != null) {
			sql.append(" and ACTIVE_RESULT=?");
		}
		if (equiptMonitor.getActiveDetail() != null) {
			sql.append(" and ACTIVE_DETAIL=?");
		}
		return sql.toString();
	}
}

